﻿using System.Diagnostics;
using System.Text;
using U8DllPack.Model;

namespace U8DllPack.Controller
{
    public static class BaseCls
    {
        /// <summary>
        /// 当前目录
        /// </summary>
        public static readonly string CurrentDir = Application.StartupPath;
        /// <summary>
        /// 链接库目录
        /// </summary>

        public static readonly string LibsDir = Path.Combine(BaseCls.CurrentDir, "Libs");
        /// <summary>
        /// 压缩文件临时目录
        /// </summary>

        public static readonly string ZipTempDir = Path.Combine(BaseCls.CurrentDir, "Temp");
        /// <summary>
        /// 项目列表
        /// </summary>
        public static readonly string ProjectListfile = "Userproject.json";
        /// <summary>
        /// 项目列表实例
        /// </summary>

        public static List<Json_Project>? JsonProject = null;
        /// <summary>
        /// 异步写log文件
        /// </summary>
        /// <param name="msg">消息</param>
        public static void WriteLog(string msg)
        {
            string text = CurrentDir + "log\\";
            bool flag = !Directory.Exists(text);
            if (flag)
            {
                Directory.CreateDirectory(text);
            }
            string str = DateTime.Now.ToString("【HH:mm:ss:ffff】");
            msg = str + msg + "\r\n";
            string path = text + DateTime.Now.ToString("yyyyMMdd") + ".log";
            byte[] bytes = Encoding.GetEncoding("utf-8").GetBytes(msg.ToString());
            FileStream fileStream = new(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
            fileStream.Seek(0L, SeekOrigin.End);
            fileStream.BeginWrite(bytes, 0, bytes.Length, new AsyncCallback(EndWriteLog), fileStream);
        }
        /// <summary>
        /// 异步写入
        /// </summary>
        /// <param name="asr">异步状态</param>
        private static void EndWriteLog(IAsyncResult asr)
        {
            using Stream? stream = (Stream?)asr.AsyncState;
            stream?.EndWrite(asr);
        }


        public static File_VersionInfo? GetVersions(string sFileNameWithPath)
        {
            if (sFileNameWithPath.IsNullOrWhiteSpace()) return null;
            try
            {
                FileVersionInfo s = FileVersionInfo.GetVersionInfo(sFileNameWithPath!);
                File_VersionInfo? vInfo = new()
                {
                    FileNameWithPath = sFileNameWithPath,
                    FileName = Path.GetFileName(sFileNameWithPath),
                    FileVersion = s.FileVersion,
                    ProductVersion = s.ProductVersion,
                    FileDescription = s.FileDescription,
                };
                return vInfo;
            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 写项目列表配置
        /// </summary>
        /// <param name="json"></param>
        public static void WriteProjJson(string json)
        {
            string fullname = Path.Combine(BaseCls.CurrentDir, BaseCls.ProjectListfile);
            File.WriteAllText(fullname, json);
        }
        /// <summary>
        /// 写单项目配置文件
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <param name="json">Json字符串</param>
        public static void WriteLibsJson(string filename, string json)
        {
            if (!Directory.Exists(BaseCls.LibsDir))
            {
                Directory.CreateDirectory(BaseCls.LibsDir);
            }
            string fullname = Path.Combine(BaseCls.LibsDir, filename);
            File.WriteAllText(fullname, json);
        }
        /// <summary>
        /// 写打包配置表
        /// </summary>
        /// <param name="json"></param>
        public static void WriteVerJson(string json)
        {
            string fullname = Path.Combine(BaseCls.ZipTempDir, "ver.json");
            File.WriteAllText(fullname, json);
        }
        /// <summary>
        /// 读项目配置列表
        /// </summary>
        /// <returns></returns>
        public static string ReadProjJson()
        {
            string fullname = Path.Combine(BaseCls.CurrentDir, BaseCls.ProjectListfile);
            if (!File.Exists(fullname)) return string.Empty;
            string json = File.ReadAllText(fullname);
            return json;
        }
        /// <summary>
        /// 读取链接库单项目配置文件
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <returns></returns>
        public static string ReadLibsJson(string filename)
        {
            string fullname = Path.Combine(BaseCls.LibsDir, filename);
            if (!File.Exists(fullname)) return string.Empty;
            string json = File.ReadAllText(fullname);
            return json;
        }
        /// <summary>
        /// 删除链接库单项目配置文件
        /// </summary>
        /// <param name="filename"></param>
        public static void DeleteLibsFile(string filename)
        {
            string fullname = Path.Combine(BaseCls.LibsDir, filename);
            if (File.Exists(fullname))
            {
                File.Delete(fullname);
            }
        }


    }
}
